<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="概述总共24种设计模式，6个创建型模式，7个结构型模式，11个行为型模式。 https://blog.csdn.net/liang19890820/article/category/6783147 创建型模式 简单工厂模式-Simple Factory Pattern【学习难度：★★☆☆☆，使用频率：★★★☆☆】  工厂方法模式-Factory Method Pattern【学习难度：★★☆☆☆，">
<meta name="keywords" content="c++设计模式">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式">
<meta property="og:url" content="http://yoursite.com/2019/06/05/设计模式/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="概述总共24种设计模式，6个创建型模式，7个结构型模式，11个行为型模式。 https://blog.csdn.net/liang19890820/article/category/6783147 创建型模式 简单工厂模式-Simple Factory Pattern【学习难度：★★☆☆☆，使用频率：★★★☆☆】  工厂方法模式-Factory Method Pattern【学习难度：★★☆☆☆，">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast-20190605225728033.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast-20190605225654593.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast-20190605225823280.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast-20190605225839131.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/20170505195903712.png">
<meta property="og:image" content="http://yoursite.com/2019/06/05/设计模式/20170505195913620.png">
<meta property="og:updated_time" content="2019-10-20T13:23:23.049Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="设计模式">
<meta name="twitter:description" content="概述总共24种设计模式，6个创建型模式，7个结构型模式，11个行为型模式。 https://blog.csdn.net/liang19890820/article/category/6783147 创建型模式 简单工厂模式-Simple Factory Pattern【学习难度：★★☆☆☆，使用频率：★★★☆☆】  工厂方法模式-Factory Method Pattern【学习难度：★★☆☆☆，">
<meta name="twitter:image" content="http://yoursite.com/2019/06/05/设计模式/SouthEast.png">
  <link rel="canonical" href="http://yoursite.com/2019/06/05/设计模式/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>设计模式 | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/05/设计模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">设计模式

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-06-05 21:05:14" itemprop="dateCreated datePublished" datetime="2019-06-05T21:05:14+00:00">2019-06-05</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>总共24种设计模式，6个创建型模式，7个结构型模式，11个行为型模式。</p>
<p><a href="https://blog.csdn.net/liang19890820/article/category/6783147" target="_blank" rel="noopener">https://blog.csdn.net/liang19890820/article/category/6783147</a></p>
<h3 id="创建型模式"><a href="#创建型模式" class="headerlink" title="创建型模式"></a>创建型模式</h3><ol>
<li><p>简单工厂模式-Simple Factory Pattern【学习难度：★★☆☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>工厂方法模式-Factory Method Pattern【学习难度：★★☆☆☆，使用频率：★★★★★】</p>
</li>
<li><p>抽象工厂模式-Abstract  Factory Pattern【学习难度：★★★★☆，使用频率：★★★★★】</p>
</li>
<li><p>单例模式-Singleton Pattern【学习难度：★☆☆☆☆，使用频率：★★★★☆】</p>
</li>
<li><p>原型模式-Prototype Pattern【学习难度：★★★☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>建造者模式-Builder Pattern【学习难度：★★★★☆，使用频率：★★☆☆☆】</p>
</li>
</ol>
<h3 id="结构型模式"><a href="#结构型模式" class="headerlink" title="结构型模式"></a>结构型模式</h3><ol start="7">
<li><p>适配器模式-Adapter Pattern【学习难度：★★☆☆☆，使用频率：★★★★☆】</p>
</li>
<li><p>桥接模式-Bridge Pattern【学习难度：★★★☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>组合模式-Composite Pattern【学习难度：★★★☆☆，使用频率：★★★★☆】</p>
</li>
<li><p>装饰模式-Decorator Pattern【学习难度：★★★☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>外观模式-Facade Pattern【学习难度：★☆☆☆☆，使用频率：★★★★★】</p>
</li>
<li><p>享元模式-Flyweight Pattern【学习难度：★★★★☆，使用频率：★☆☆☆☆】</p>
</li>
<li><p>代理模式-Proxy Pattern【学习难度：★★★☆☆，使用频率：★★★★☆】</p>
</li>
</ol>
<h3 id="行为型模式"><a href="#行为型模式" class="headerlink" title="行为型模式"></a>行为型模式</h3><p>14 . 职责链模式-Chain of Responsibility Pattern【学习难度：★★★☆☆，使用频率：★★☆☆☆】</p>
<ol start="15">
<li><p>命令模式-Command Pattern【学习难度：★★★☆☆，使用频率：★★★★☆】</p>
</li>
<li><p>解释器模式-Interpreter Pattern【学习难度：★★★★★，使用频率：★☆☆☆☆】</p>
</li>
<li><p>迭代器模式-Iterator Pattern【学习难度：★★★☆☆，使用频率：★★★★★】</p>
</li>
<li><p>中介者模式-Mediator Pattern【学习难度：★★★☆☆，使用频率：★★☆☆☆】</p>
</li>
<li><p>备忘录模式-Memento Pattern【学习难度：★★☆☆☆，使用频率：★★☆☆☆】</p>
</li>
<li><p>观察者模式-Observer Pattern【学习难度：★★★☆☆，使用频率：★★★★★】</p>
</li>
<li><p>状态模式-State Pattern【学习难度：★★★☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>策略模式-Strategy Pattern【学习难度：★☆☆☆☆，使用频率：★★★★☆】</p>
</li>
<li><p>模板方法模式-Template Method Pattern【学习难度：★★☆☆☆，使用频率：★★★☆☆】</p>
</li>
<li><p>访问者模式-Visitor Pattern【学习难度：★★★★☆，使用频率：★☆☆☆☆】</p>
</li>
</ol>
<a id="more"></a>

<h3 id="最常用的创建型模式-gt-★★★★"><a href="#最常用的创建型模式-gt-★★★★" class="headerlink" title="最常用的创建型模式  &gt; ★★★★"></a>最常用的创建型模式  &gt; ★★★★</h3><h4 id="工厂方法模式-Factory-Method-Pattern"><a href="#工厂方法模式-Factory-Method-Pattern" class="headerlink" title="工厂方法模式-Factory Method Pattern"></a>工厂方法模式-Factory Method Pattern</h4><p><strong>简述</strong></p>
<p>此模式的核心思想是封装类中不变的部分，提取其中个性化善变的部分为独立类，通过依赖注入以达到解耦、复用以及方便后期维护拓展的目的。</p>
<p><strong>模式结构</strong></p>
<p><img src="/2019/06/05/设计模式/SouthEast.png" alt="Factory Method Pattern"></p>
<ul>
<li><p>Factory（抽象工厂）：是工厂方法模式的核心，与应用程序无关。任何在模式中创建的对象的工厂类必须实现这个接口。</p>
</li>
<li><p>ConcreteFactory（具体工厂）：实现抽象工厂接口的具体工厂类，包含与应用程序密切相关的逻辑，并且被应用程序调用以创建产品对象。</p>
</li>
<li><p>Product（抽象产品）：所创建对象的基类，也就是具体产品的共同父类或共同拥有的接口。</p>
</li>
<li><p>ConcreteProduct（具体产品）：实现了抽象产品角色所定义的接口。某具体产品有专门的具体工厂创建，它们之间往往一一对应。</p>
</li>
</ul>
<p><strong>优点</strong></p>
<p>工厂和产品都是解耦的。</p>
<p><strong>缺点</strong></p>
<p>每增加一个产品，需要增加一个子工厂，增加了开销。</p>
<p><strong>适用场景</strong></p>
<ul>
<li>对于某个产品，调用者清楚地知道应该使用哪个具体工厂服务，实例化该具体工厂，生产出具体的产品来。</li>
<li>只是需要一种产品，而不想知道也不需要知道究竟是哪个工厂生产的，即最终选用哪个具体工厂的决定权在生产者一方，它们根据当前系统的情况来实例化一个具体的工厂返回给使用者，而这个决策过程对于使用者来说是透明的。</li>
</ul>
<p><strong>案例代码实现</strong></p>
<p><strong>产品</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ICar</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BenzCar</span> :</span> <span class="keyword">public</span> ICar</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Benz Car"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BmwCar</span> :</span> <span class="keyword">public</span> ICar</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Bmw Car"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><strong>工厂</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">class <span class="title">AFactory</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> ICar* <span class="title">CreateCar</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BenzFactory</span> :</span> <span class="keyword">public</span> AFactory</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="keyword">virtual</span> ICar* CreateCar &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BenzCar();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BmwFactory</span> :</span> <span class="keyword">public</span> AFactory</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> ICar* <span class="title">CreateCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BmwCar();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>客户端使用</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SAFE_DELETE(p) &#123; <span class="meta-keyword">if</span>(p)&#123;delete(p); (p)=NULL;&#125; &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  AFactory* pFactory = <span class="keyword">new</span> BenzFactory();</span><br><span class="line">  ICar* pCar = pFactory-&gt;CreateCar();</span><br><span class="line">  <span class="built_in">cout</span> &lt;&lt; pCar-&gt;Name() &lt;&lt; <span class="built_in">endl</span>; <span class="comment">// "Benz Car"</span></span><br><span class="line">  </span><br><span class="line">  SAFE_DELETE(pFactory);</span><br><span class="line">  SAFE_DELETE(pCar);</span><br><span class="line">  </span><br><span class="line">  AFactory* pFactory = <span class="keyword">new</span> BmwFactory();</span><br><span class="line">  ICar* pCar = pFactory-&gt;CreateCar();</span><br><span class="line">  <span class="built_in">cout</span> &lt;&lt; pCar-&gt;Name() &lt;&lt; <span class="built_in">endl</span>; <span class="comment">// "Bmw Car"</span></span><br><span class="line">  </span><br><span class="line">  SAFE_DELETE(pFactory);</span><br><span class="line">  SAFE_DELETE(pCar);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="抽象工厂模式-Abstract-Factory-Pattern"><a href="#抽象工厂模式-Abstract-Factory-Pattern" class="headerlink" title="抽象工厂模式-Abstract  Factory Pattern"></a>抽象工厂模式-Abstract  Factory Pattern</h4><p><strong>简述</strong></p>
<p>工厂抽象化，一个工厂接口可以创建不同的工厂生产不同类别的产品</p>
<p><strong>模式结构</strong></p>
<p><img src="/2019/06/05/设计模式/SouthEast-20190605225728033.png" alt="Abstract Factory Pattern"></p>
<ul>
<li>Factory（抽象工厂）：声明一个用于创建抽象产品的接口</li>
<li>ConcreteFactory（具体工厂）：用于创建具体的产品</li>
<li>Product（抽象产品）：声明一个产品对象类型的接口</li>
<li>ConcreteProduct（具体产品）：由具体工厂创建的具体产品</li>
</ul>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p>使用方不需要知道具体那种产品，只需要知道工厂名就可以了</p>
<p>同一类型，不同产品管理方便</p>
<p><strong>缺点</strong></p>
<p>过于臃肿，添加一种产品，所有工厂类都必须加一个方法。</p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<p>BBA工厂还可以生产自行车</p>
<p><strong>产品</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ICar</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BenzCar</span> :</span> <span class="keyword">public</span> ICar</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Benz Car"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BmwCar</span> :</span> <span class="keyword">public</span> ICar</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Bmw Car"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">IBike</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> 	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BenzBike</span> :</span> <span class="keyword">public</span> IBike</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Benz Bike"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BmwBike</span> :</span> <span class="keyword">public</span> IBike</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">Name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Bmw Bike"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><strong>创建抽象工厂</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// .h</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AFactory</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="keyword">enum</span> FACTORY_TYPE &#123;</span><br><span class="line">    BENZ_FACTORY,</span><br><span class="line">    BMW_FACTORY,</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> ICar* <span class="title">CreateCar</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> IBike* <span class="title">CreateBike</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">  <span class="function"><span class="keyword">static</span> AFactory* <span class="title">CreateFactory</span><span class="params">(FACTORY_TYPE factory)</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// .cpp</span></span><br><span class="line">AFactory* AFactory::CreateFactory(FACTORY_TYPE factory) </span><br><span class="line">&#123;</span><br><span class="line">   <span class="keyword">static</span> AFactory* pFactory = <span class="literal">nullptr</span>;</span><br><span class="line">	 <span class="keyword">switch</span>(factory)</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="keyword">case</span> BENZ_FACTORY:</span><br><span class="line">       pFactory = <span class="keyword">new</span> BenzFactory();</span><br><span class="line">       <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> BMW_FACTORY:</span><br><span class="line">       pFactory = <span class="keyword">new</span> BmwFactory();</span><br><span class="line">       <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">default</span>:</span><br><span class="line">       <span class="keyword">break</span>;</span><br><span class="line">   &#125;</span><br><span class="line">  	<span class="keyword">return</span> pFactory;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>具体的工厂类</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BenzFactory</span> :</span> <span class="keyword">public</span> AFactory</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> ICar* <span class="title">CreateCar</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BenzCar();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> IBike* <span class="title">CreateBike</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BenzCar();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BmwFactory</span> :</span> <span class="keyword">public</span> AFactory</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> ICar* <span class="title">CreateCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BenzBike();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> IBike* <span class="title">CreateBike</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> BenzBike();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>客户端</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    AFactory *pFactory = AFactory::CreateFactory(AFactory::FACTORY_TYPE::BENZ_FACTORY);</span><br><span class="line">    ICar *pCar = pFactory-&gt;CreateCar();</span><br><span class="line">    IBike *pBike = pFactory-&gt;CreateBike();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Benz factory - Car: "</span> &lt;&lt; pCar-&gt;Name() &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Benz factory - Bike: "</span> &lt;&lt; pBike-&gt;Name() &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    SAFE_DELETE(pCar);</span><br><span class="line">    SAFE_DELETE(pBike);</span><br><span class="line">    SAFE_DELETE(pFactory);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="单例模式-Singleton-Pattern"><a href="#单例模式-Singleton-Pattern" class="headerlink" title="单例模式-Singleton Pattern"></a>单例模式-Singleton Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><img src="/2019/06/05/设计模式/SouthEast-20190605225654593.png" alt="Singleton Pattern"></p>
<hr>
<h3 id="最常用的结构型模式-gt-★★★★"><a href="#最常用的结构型模式-gt-★★★★" class="headerlink" title="最常用的结构型模式  &gt; ★★★★"></a>最常用的结构型模式  &gt; ★★★★</h3><h4 id="适配器模式-Adapter-Pattern"><a href="#适配器模式-Adapter-Pattern" class="headerlink" title="适配器模式-Adapter Pattern"></a>适配器模式-Adapter Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p>UML 结构图（类适配器）：</p>
<p><img src="/2019/06/05/设计模式/SouthEast-20190605225823280.png" alt="Adapter Pattern"></p>
<p>UML 结构图（对象适配器）：</p>
<p><img src="/2019/06/05/设计模式/SouthEast-20190605225839131.png" alt="Adapter Pattern"></p>
<ul>
<li>Target（目标接口）：所期望得到的接口</li>
<li>Adaptee（适配者）：需要适配的类</li>
<li>Adapter（适配器）：该模式的核心，具有将 Adaptee 包装为 Target 的职责</li>
</ul>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<ul>
<li>可以让任何两个没有关联的类一起运行</li>
<li>提高了类的复用</li>
<li>增加了类的透明度</li>
<li>灵活性好</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>过多地使用适配器，会让系统非常零乱，不利于整体把控。</li>
</ul>
<p><strong>适用场景</strong></p>
<ul>
<li>当想使用一个已存在的类，而它的接口不符合需求时。</li>
<li>你想创建一个可复用的类，该类可以与其他不相关的类或不可预见的类协同工作。</li>
<li>你想使用一些已经存在的子类，但是不可能对每一个都进行子类化以匹配它们的接口，对象适配器可以适配它的父接口。</li>
</ul>
<p><strong>案例代码实现</strong></p>
<p><strong><em>对象适配器</em></strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 俄罗斯提供的插座</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">IRussiaSocket</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 使用双脚圆形充电（暂不实现）</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">Charge</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自带的充电器 - 两脚扁型</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OwnCharger</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">ChargeWithFeetFlat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"OwnCharger::ChargeWithFeetFlat"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 电源适配器</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PowerAdapter</span> :</span> <span class="keyword">public</span> IRussiaSocket</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    PowerAdapter() : m_pCharger(<span class="keyword">new</span> OwnCharger())&#123;&#125;</span><br><span class="line">    ~PowerAdapter() &#123;</span><br><span class="line">        SAFE_DELETE(m_pCharger);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Charge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//  使用自带的充电器（两脚扁型）充电</span></span><br><span class="line">        m_pCharger-&gt;ChargeWithFeetFlat();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    OwnCharger *m_pCharger;  <span class="comment">// 持有需要被适配的接口对象 - 自带的充电器</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 创建适配器</span></span><br><span class="line">    IRussiaSocket *pAdapter = <span class="keyword">new</span> PowerAdapter();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 充电</span></span><br><span class="line">    pAdapter-&gt;Charge();</span><br><span class="line"></span><br><span class="line">    SAFE_DELETE(pAdapter);</span><br><span class="line"></span><br><span class="line">    getchar();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong><em>类适配器</em></strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 电源适配器</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PowerAdapter</span> :</span> <span class="keyword">public</span> IRussiaSocket, OwnCharger</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    PowerAdapter() &#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Charge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 使用自带的充电器（两脚扁型）充电</span></span><br><span class="line">        ChargeWithFeetFlat();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="组合模式-Composite-Pattern"><a href="#组合模式-Composite-Pattern" class="headerlink" title="组合模式-Composite Pattern"></a>组合模式-Composite Pattern</h4><p><strong>简述</strong></p>
<p>组合模式（Composite Pattern）组合多个对象形成树形结构以表示具有“整体-部分”关系的层次结构。组合模式对单个对象（即：叶子构件）和组合对象（即：容器构件）的使用具有一致性，组合模式又被称为“整体-部分”（Part-Whole）模式，属于对象结构型模式。</p>
<p><strong>模式结构</strong></p>
<p>UML 结构图（透明组合模式）</p>
<p><img src="/2019/06/05/设计模式/20170505195903712.png" alt="Composite Pattern"></p>
<p>UML 结构图（安全组合模式）</p>
<p><img src="/2019/06/05/设计模式/20170505195913620.png" alt="Composite Pattern"></p>
<ul>
<li><p>Component（抽象构件）：为叶子构件和容器构件对象定义接口，可以包含所有子类共有行为的声明和实现。在抽象构件中，声明了访问及管理子构件的接口（例如：Add()、Remove()、GetChild() 等）。</p>
</li>
<li><p>Leaf（叶子构件）：叶子节点没有子节点。它实现了 Component 中定义的行为，对于访问及管理子构件的接口，可以通过异常等方式进行处理。</p>
</li>
<li><p>Composite（容器构件）：容器节点包含子节点（可以是叶子构件，也可以是容器构件）。它提供了一个集合用于存储子节点，实现了 Component 中定义的行为，包括访问及管理子构件的接口，在其业务方法中可以递归调用其子节点的业务方法。</p>
</li>
</ul>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<ul>
<li>组合模式可以清楚地定义分层次的复杂对象，表示对象的全部或部分层次，它让 Client 忽略了层次的差异，方便对整个层次结构进行控制。</li>
<li>Client 可以一致地使用一个组合结构或其中单个对象，不必关心处理的是单个对象还是整个组合结构，简化了 Client 的代码。</li>
<li>在组合模式中，增加新的叶子构件和容器构件很方便，无须对现有类进行任何修改，符合“开闭原则”。</li>
<li>为树形结构提供了一种灵活的解决方案，通过递归组合容器对象和叶子对象，可以形成复杂的树形结构，但对树形结构的控制却非常简单。</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>使设计变得更加抽象，对象的业务规则如果很复杂，则实现组合模式具有很大挑战性，而且不是所有的方法都与叶子对象子类都有关联。</li>
</ul>
<p><strong>适用场景</strong></p>
<ul>
<li>表示对象的“整体-部分”层次结构（树形结构）</li>
<li>希望用户忽略组合对象与单个对象的不同，统一地使用组合结构中的所有对象。</li>
</ul>
<p><strong>案例代码实现</strong></p>
<p>Component 需要定义访问及管理子构件的接口</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Component</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Component(<span class="built_in">string</span> name) : m_strName(name) &#123;&#125;</span><br><span class="line">    <span class="keyword">virtual</span> ~Component() &#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">Add</span><span class="params">(Component *cmpt)</span> </span>= <span class="number">0</span>;  <span class="comment">// 添加构件</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">Remove</span><span class="params">(Component *cmpt)</span> </span>= <span class="number">0</span>;  <span class="comment">// 删除构件</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> Component* <span class="title">GetChild</span><span class="params">(<span class="keyword">int</span> index)</span> </span>= <span class="number">0</span>;  <span class="comment">// 获取构件</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">Operation</span><span class="params">(<span class="keyword">int</span> indent)</span> </span>= <span class="number">0</span>;  <span class="comment">// 显示构件（以缩进的方式）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    Component();  <span class="comment">// 不允许</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">    <span class="built_in">string</span> m_strName;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">作为 Component 的子类，Leaf 需要实现 Component 中定义的所有接口，但是 Leaf 不能包含子构件。因此，在 Leaf 中实现访问和管理子构件的函数时，需要进行异常处理或错误提示。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Leaf</span> :</span> <span class="keyword">public</span> Component</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Leaf(<span class="built_in">string</span> name) : Component(name)&#123;&#125;</span><br><span class="line">    <span class="keyword">virtual</span> ~Leaf()&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(Component *cmpt)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Can't add to a Leaf"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Remove</span><span class="params">(Component *cmpt)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Can't remove from a Leaf"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">Component* <span class="title">GetChild</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Can't get child from a Leaf"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Operation</span><span class="params">(<span class="keyword">int</span> indent)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">string</span> <span class="title">newStr</span><span class="params">(indent, <span class="string">'-'</span>)</span></span>;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; newStr &lt;&lt; <span class="string">" "</span> &lt;&lt; m_strName &lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    Leaf();  <span class="comment">// 不允许</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Composite</span> :</span> <span class="keyword">public</span> Component</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Composite (<span class="built_in">string</span> name) : Component(name) &#123;&#125;</span><br><span class="line">    <span class="keyword">virtual</span> ~Composite() &#123;</span><br><span class="line">        <span class="keyword">while</span> (!m_elements.empty()) &#123;</span><br><span class="line">            <span class="built_in">vector</span>&lt;Component*&gt;::iterator it = m_elements.begin();</span><br><span class="line">            SAFE_DELETE(*it);</span><br><span class="line">            m_elements.erase(it);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(Component *cmpt)</span> </span>&#123;</span><br><span class="line">        m_elements.push_back(cmpt);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Remove</span><span class="params">(Component *cmpt)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;Component*&gt;::iterator it = m_elements.begin();</span><br><span class="line">        <span class="keyword">while</span> (it != m_elements.end())  &#123;</span><br><span class="line">            <span class="keyword">if</span> (*it == cmpt) &#123;</span><br><span class="line">                SAFE_DELETE(cmpt);</span><br><span class="line">                m_elements.erase(it);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            ++it;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">Component* <span class="title">GetChild</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= m_elements.size())</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> m_elements[index];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 递归显示</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Operation</span><span class="params">(<span class="keyword">int</span> indent)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">string</span> <span class="title">newStr</span><span class="params">(indent, <span class="string">'-'</span>)</span></span>;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; newStr &lt;&lt; <span class="string">"+ "</span> &lt;&lt; m_strName &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="comment">// 显示每个节点的孩子</span></span><br><span class="line">        <span class="built_in">vector</span>&lt;Component*&gt;::iterator it = m_elements.begin();</span><br><span class="line">        <span class="keyword">while</span> (it != m_elements.end()) &#123;</span><br><span class="line">            (*it)-&gt;Operation(indent + <span class="number">2</span>);</span><br><span class="line">            ++it;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    Composite();  <span class="comment">// 不允许</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;Component *&gt; m_elements;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="外观模式-Facade-Pattern"><a href="#外观模式-Facade-Pattern" class="headerlink" title="外观模式-Facade Pattern"></a>外观模式-Facade Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="代理模式-Proxy-Pattern"><a href="#代理模式-Proxy-Pattern" class="headerlink" title="代理模式-Proxy Pattern"></a>代理模式-Proxy Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h3 id="最常用的行为型模式-gt-★★★★"><a href="#最常用的行为型模式-gt-★★★★" class="headerlink" title="最常用的行为型模式  &gt; ★★★★"></a>最常用的行为型模式  &gt; ★★★★</h3><h4 id="命令模式-Command-Pattern"><a href="#命令模式-Command-Pattern" class="headerlink" title="命令模式-Command Pattern"></a>命令模式-Command Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="迭代器模式-Iterator-Pattern"><a href="#迭代器模式-Iterator-Pattern" class="headerlink" title="迭代器模式-Iterator Pattern"></a>迭代器模式-Iterator Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="观察者模式-Observer-Pattern"><a href="#观察者模式-Observer-Pattern" class="headerlink" title="观察者模式-Observer Pattern"></a>观察者模式-Observer Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="策略模式-Strategy-Pattern"><a href="#策略模式-Strategy-Pattern" class="headerlink" title="策略模式-Strategy Pattern"></a>策略模式-Strategy Pattern</h4><p><strong>简述</strong></p>
<p><strong>模式结构</strong></p>
<p><strong>优缺点</strong></p>
<p><strong>优点</strong></p>
<p><strong>缺点</strong></p>
<p><strong>适用场景</strong></p>
<p><strong>案例代码实现</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/c-设计模式/" rel="tag"># c++设计模式</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2019/06/05/C++新特性/" rel="next" title="C++常用新特性">
                  <i class="fa fa-chevron-left"></i> C++常用新特性
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2019/06/12/机器学习/" rel="prev" title="机器学习之支持向量机(SVM)">
                  机器学习之支持向量机(SVM) <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建型模式"><span class="nav-number">1.1.</span> <span class="nav-text">创建型模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构型模式"><span class="nav-number">1.2.</span> <span class="nav-text">结构型模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#行为型模式"><span class="nav-number">1.3.</span> <span class="nav-text">行为型模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最常用的创建型模式-gt-★★★★"><span class="nav-number">1.4.</span> <span class="nav-text">最常用的创建型模式  &gt; ★★★★</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#工厂方法模式-Factory-Method-Pattern"><span class="nav-number">1.4.1.</span> <span class="nav-text">工厂方法模式-Factory Method Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#抽象工厂模式-Abstract-Factory-Pattern"><span class="nav-number">1.4.2.</span> <span class="nav-text">抽象工厂模式-Abstract  Factory Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#单例模式-Singleton-Pattern"><span class="nav-number">1.4.3.</span> <span class="nav-text">单例模式-Singleton Pattern</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最常用的结构型模式-gt-★★★★"><span class="nav-number">1.5.</span> <span class="nav-text">最常用的结构型模式  &gt; ★★★★</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#适配器模式-Adapter-Pattern"><span class="nav-number">1.5.1.</span> <span class="nav-text">适配器模式-Adapter Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#组合模式-Composite-Pattern"><span class="nav-number">1.5.2.</span> <span class="nav-text">组合模式-Composite Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#外观模式-Facade-Pattern"><span class="nav-number">1.5.3.</span> <span class="nav-text">外观模式-Facade Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代理模式-Proxy-Pattern"><span class="nav-number">1.5.4.</span> <span class="nav-text">代理模式-Proxy Pattern</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最常用的行为型模式-gt-★★★★"><span class="nav-number">1.6.</span> <span class="nav-text">最常用的行为型模式  &gt; ★★★★</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#命令模式-Command-Pattern"><span class="nav-number">1.6.1.</span> <span class="nav-text">命令模式-Command Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#迭代器模式-Iterator-Pattern"><span class="nav-number">1.6.2.</span> <span class="nav-text">迭代器模式-Iterator Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#观察者模式-Observer-Pattern"><span class="nav-number">1.6.3.</span> <span class="nav-text">观察者模式-Observer Pattern</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#策略模式-Strategy-Pattern"><span class="nav-number">1.6.4.</span> <span class="nav-text">策略模式-Strategy Pattern</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
